<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '@/api'
import { Message } from '@arco-design/web-vue'
import UploadImage from '@/components/uploadImage.vue'

const $route = useRoute()
const $router = useRouter()

const keyword = ref('')
const pageVal = ref(1)
const pageSize = ref(10)
const listTotal = ref(0)
const listData = ref()
const formVisible = ref(false)
const formData = ref({
  id: null,
  title: '',
  icon: '',
})

const getListData = async (page) => {
  try {
    const res = await api.get('system/icon/list', {
      params: {
        keyword: keyword.value,
        page,
        pageSize: pageSize.value,
      },
    })
    listData.value = res.data.items
    listTotal.value = res.data.pageInfo.total
    pageVal.value = page
  } catch (error) {
    console.error(error)
  }
}

const showFormData = async (record) => {
  if (record) {
    formData.value.id = record.id
    formData.value.title = record.title
    formData.value.icon = record.icon
  } else {
    resetFormData()
  }
  formVisible.value = true
}

const formSubmit = async () => {
  var obj = { ...formData.value }
  if (!obj.title) {
    Message.error('标题不可为空')
    return false
  }
  if (!obj.icon) {
    Message.error('图标不可为空')
    return false
  }
  if (formData.value.id) {
    try {
      const res = await api.post('system/icon/update/' + obj.id, obj)
      Message.success(res.message)
      getListData(1)
      resetFormData()
    } catch (error) {
      console.error('error', error)
      return false
    }
  } else {
    try {
      const res = await api.post('system/icon/create', obj)
      Message.success(res.message)
      formVisible.value = false
      getListData(1)
      resetFormData()
    } catch (error) {
      console.error('error', error)
      return false
    }
  }
}

const resetFormData = async () => {
  formData.value = {
    id: null,
    title: '',
    icon: '',
  }
}

const deleteItem = async (id) => {
  try {
    const res = await api.get('system/icon/del/' + id)
    Message.success(res.message)
    getListData(1)
  } catch (error) {
    console.error('error', error)
    return false
  }
}

getListData(1)
</script>

<template>
  <div class="page">
    <div class="header">
      <a-space>
        <a-input :style="{ width: '240px' }"
                 v-model="keyword"
                 placeholder="按标题查询"
                 allow-clear />

        <a-button @click="getListData(1)"
                  type="primary">查询</a-button>
      </a-space>

      <a-button @click="showFormData()"
                type="primary">新增背景图</a-button>
    </div>

    <a-table :data="listData"
             @page-change="getListData"
             :pagination="{
              showTotal: true,
              size: 'small',
              total: listTotal,
              pageSize: pageSize,
              current: pageVal,
            }">
      <template #columns>
        <a-table-column title="序号"
                        :width="60">
          <template #cell="{ rowIndex }"> {{ rowIndex + 1 }} </template>
        </a-table-column>
        <a-table-column title="标题"
                        data-index="title"></a-table-column>
        <a-table-column title="图标">
          <template #cell="{ record }">
            <a-image width="100"
                     height="100"
                     fit="cover"
                     :src="record.icon" />
          </template>
        </a-table-column>
        <a-table-column title="创建时间"
                        data-index="created_at"></a-table-column>
        <a-table-column title="修改时间"
                        data-index="updated_at"></a-table-column>
        <a-table-column title="操作"
                        align="center"
                        :width="300">
          <template #cell="{ record }">
            <a-space>
              <a-popconfirm @ok="deleteItem(record.id)"
                            content="确认删除该数据吗?"
                            type="warning">
                <a-button type="primary"
                          status="danger"
                          size="mini">删除</a-button>
              </a-popconfirm>
              <a-button @click="showFormData(record)"
                        type="primary"
                        size="mini">编辑</a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <a-modal :title="formData.id ? '编辑背景图':'新增背景图'"
             width="600px"
             :on-before-ok="formSubmit"
             v-model:visible="formVisible">
      <a-form :model="formData">
        <a-form-item label="标题">
          <a-input v-model="formData.title"
                   :style="{width:'400px'}"
                   placeholder="请输入标题"
                   allow-clear></a-input>
        </a-form-item>
        <a-form-item label="背景图">
          <div style="width: 400px">
            <UploadImage width="200px"
                         height="200px"
                         v-model="formData.icon"></UploadImage>
          </div>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style scoped>
.page {
  height: 100%;
}

.header {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>